// 用户数据模块
import http from '@/shouyun/request/index'
import store from '@/shouyun/store'

const state = {
	token: uni.getStorageSync("token") || "",
	isLogin: uni.getStorageSync("isLogin") || false, // 是否登陆
	userInfo: uni.getStorageSync("userInfo") || {}, // 用户信息
	userData: {}, //用户其他相关信息
	authType: '' // accountLogin:密码登录, forgotPwd:忘记密码
}

const getters = {
	token: state => state.token,
	isLogin: state => state.isLogin,
	userInfo: state => state.userInfo,
	userOtherData: state => state.userData,
	authType: state => state.authType,
}

const actions = {
	// 获取用户信息
	getUserInfo({
		commit
	}, userInfo) {
		let token = userInfo.token
		token && uni.setStorageSync('token', token);
		commit('userInfo', userInfo);
		commit('isLogin', true);
	},
	
	setUserInfo({
		commit
	}, userInfo) {
		commit('userInfo', userInfo)
	},
	
	// 用户其他相关信息
	getUserData({
		commit
	}) {
		return new Promise((resolve, reject) => {
			http('user.userData').then(res => {
				commit('USER_DATA', res.data);
				resolve(res)
			}).catch(e => {
				reject(e)
			})
		})
	},

	// 退出登录
	logout({
		commit,
		dispatch
	}) {
		commit('token', "");
		commit('isLogin', false);
		commit('userInfo', {});
		commit('USER_DATA', {});
	},
}

const mutations = {
	token(state, payload) {
		state.token = payload;
		uni.setStorageSync("token", payload);
	},
	// 登录态
	isLogin(state, data) {
		state.isLogin = data;
		uni.setStorageSync('isLogin', data);
	},
	// 用户信息
	userInfo(state, data) {
		state.userInfo = data;
		uni.setStorageSync("userInfo", data);
	},
	USER_DATA(state, data) {
		state.userData = data;
	},
}



export default {
	state,
	mutations,
	actions,
	getters
}
